package com.example.dnfgamercommunitybackend.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Component
public class RedisUtil {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public void set(String key,Object value){
        try{
            ValueOperations<String,Object> valueOperations = redisTemplate.opsForValue();
            valueOperations.set(key,value);
        }catch (Exception e){
            throw new RuntimeException("Redis-信息保存失败");
        }
    }

    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    public Boolean exists(String key){
        return redisTemplate.hasKey(key);
    }

    public Boolean remove(String key){
        if (exists(key)){
           return redisTemplate.delete(key);
        }
        return false;
    }
}
